//
//  HMRefreshControl.m
//  kuangjia
//
//  Created by 张帅 on 15/10/4.
//  Copyright © 2015年 ZhangShuai. All rights reserved.
//

#import "HMRefreshControl.h"
#define HMRefreshControlMaxOffset -60
@interface HMRefreshControl ()

@property(nonatomic,strong) HMRefreshView *refreshView;

@end

@implementation HMRefreshControl

//停止刷新

- (void)endRefreshing{
    [super endRefreshing];

    [self.refreshView stopAnimation];
}


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{

    
    if (self.frame.origin.y>0) {
        return;
    }
    
    
///  Description 判断是不是在shuaxin
    
    if (self.isRefreshing) {
        [self.refreshView loadingAnimation];
        return;
    }
    
    
    
    if (self.frame.origin.y<  HMRefreshControlMaxOffset&& !self.refreshView.rotateFlag) {
        
        NSLog(@"反过来");
        self.refreshView.rotateFlag =YES;
        
    }
    
    if (self.frame.origin.y >=HMRefreshControlMaxOffset&& self.refreshView.rotateFlag) {
        
        NSLog(@"转过来");
        self.refreshView.rotateFlag =NO;
        
    }
    
//    NSLog(@"%@",NSStringFromCGRect(self.frame) );
    
    
}


-(void)dealloc{
    [self removeObserver:self forKeyPath:@"frame"];

}
-(instancetype)init{

    if (self==[super init]) {
        
        [self setupUI];
    }
    return self;
}


-(void)setupUI{
    
    
    [self addObserver:self forKeyPath:@"frame" options:0 context:nil];
    
    
    self.tintColor = [UIColor clearColor];
    [self addSubview:self.refreshView];
    
 
    self.refreshView.translatesAutoresizingMaskIntoConstraints=NO;
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.refreshView attribute:(NSLayoutAttributeCenterX) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeCenterX) multiplier:1 constant:0]];

    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.refreshView attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeTop) multiplier:1 constant:0]];
    
//    
//    //大小
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.refreshView attribute:(NSLayoutAttributeWidth) relatedBy:(NSLayoutRelationEqual) toItem:nil attribute:(NSLayoutAttributeWidth) multiplier:1 constant:self.refreshView.bounds.size.width]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.refreshView attribute:(NSLayoutAttributeHeight) relatedBy:(NSLayoutRelationEqual) toItem:nil attribute:(NSLayoutAttributeHeight) multiplier:1 constant:self.refreshView.bounds.size.height]];
    
}

- (HMRefreshView* ) refreshView{
    if (_refreshView==nil) {
        
        _refreshView=[HMRefreshView refreshView];
    
    }
    return _refreshView;
}


@end


@interface HMRefreshView ()

@property (weak, nonatomic) IBOutlet UIImageView *loadingIcon;

@property (weak, nonatomic) IBOutlet UIImageView *pulldownTipIcon;



@property (weak, nonatomic) IBOutlet UIView *tipView;



@end

@implementation HMRefreshView



-(void)setRotateFlag:(BOOL)rotateFlag{
    _rotateFlag=rotateFlag;
    [self rotateTipAnimation];
    
}
+( instancetype )refreshView{

    return [[[NSBundle mainBundle]loadNibNamed:@"HMRefreshView" owner:nil options:nil] lastObject] ;

}

- (void)rotateTipAnimation{

    CGFloat angle = M_PI;
    
    angle+= self.rotateFlag?-0.01: 0.01;
    
    // 在 iOS 的 block 动画中，旋转是默认顺时针，就近原则
 
    [UIView animateWithDuration:1 animations:^{
        self.pulldownTipIcon.transform = CGAffineTransformRotate(self.pulldownTipIcon.transform, angle);
    }];
}

//加载动画

-(void)loadingAnimation{

    
    // 通过 key 能够拿到图层上的动画
    NSString  *loadingKey = @"loadingKey";
    if ([self.loadingIcon.layer animationForKey:loadingKey] !=nil) {
        return;
    }
    self.tipView.hidden=YES;
    
    CABasicAnimation * anim =[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    anim.toValue =  @(2 * M_PI);
    
    anim.repeatCount= CGFLOAT_MAX;
    anim.duration= 1;
    
    [self.loadingIcon.layer addAnimation:anim forKey:loadingKey];
    
}

  /// 停止动画
-(void)stopAnimation{

    self.tipView.hidden=NO;
    
    [self.loadingIcon.layer removeAllAnimations];
}

@end


